package cn.egame.extraction.service;

import java.rmi.RemoteException;

import org.apache.log4j.Logger;

import cn.egame.client.EGameClientV2;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.extraction.dao.GameServiceDao;
import cn.egame.extraction.dao.GameTagLinkDao;
import cn.egame.extraction.pojo.GameStatusInfo;
import cn.egame.extraction.pojo.GameTagLinkInfo;
import cn.egame.extraction.util.ConsVar;
import cn.egame.extraction.util.DateType.RankType;
import cn.egame.extraction.util.DateType.TimeType;
import cn.egame.interfaces.EnumType.NetworkType;
import cn.egame.interfaces.gc.GameInfo;

public class GameTagLinkService {
	private static GameTagLinkService instance = null;
	private static byte[] lock = new byte[0];
	private Logger log = Logger.getLogger(GameTagLinkService.class);
	public static GameTagLinkService getInstance() throws ExceptionCommonBase {
		if (instance == null) {
			synchronized (lock) {
				if (instance == null) {
					instance = new GameTagLinkService();
				}
			}
		}
		return instance;
	}

	private GameTagLinkDao dao = null;
	private GameServiceDao gameDao = null;
	public GameTagLinkService() throws ExceptionCommonBase {
		dao = new GameTagLinkDao();
		gameDao = new GameServiceDao();
	}

	public void updateGameTagLinkDao(GameStatusInfo gameStatusInfo) {
		int gId = gameStatusInfo.getGId();
		if (gId <= 0) {
			return;
		}
		try {
			updateGameTagLink(gameStatusInfo.getCountDownDay(), gId, TimeType.day);
			updateGameTagLink(gameStatusInfo.getCountDownWeek(), gId, TimeType.week);
			updateGameTagLink(gameStatusInfo.getCountDownMonth(), gId, TimeType.month);
			updateGameTagLink(gameStatusInfo.getCountDownTotal(), gId, TimeType.total);
			updateGameTagLink(gameStatusInfo.getCountDownWeek(), gId, TimeType.free);
		} catch (Exception e) {
			log.error(null,e);
		}

	}

	private void updateGameTagLink(int gameCount, int gId,
			TimeType type) throws Exception {
	    GameTagLinkInfo gameTagLinkInfo = new GameTagLinkInfo();
	    Integer tagId = ParameterAppService.getInstance().map.get(type
                .getValue() + "");
        if (tagId == null || tagId <= 0) {
            return;
        }
		int networkType = gameDao.getGameNetworkTypeByGId(gId);
		//页游的不抽取
		if (networkType == NetworkType.webGame.value()) {
			return;
		}
      //非商用的不抽取
		GameInfo gameInfo = EGameClientV2.getInstance().getGameInfoById(0, 0,
				gId);
		if(gameInfo ==null){
		    gameTagLinkInfo.setGameSortNo(gameCount);
	        gameTagLinkInfo.setgId(gId);
	        gameTagLinkInfo.setTagId(tagId);
	        gameTagLinkInfo.setEnable(0);
	        dao.updateGameTagLinkDao(gameTagLinkInfo);
			return;
		}
		if ((gameInfo.getGameStatus().value()) != ConsVar.GAME_STATUS_ON_LINE) {
		    gameTagLinkInfo.setGameSortNo(gameCount);
            gameTagLinkInfo.setgId(gId);
            gameTagLinkInfo.setTagId(tagId);
            gameTagLinkInfo.setEnable(0);
            dao.updateGameTagLinkDao(gameTagLinkInfo);
			return;
		}
      //如果是免费排序只抽取免费的游戏
		if (type.getRankType() == RankType.free) {
			if (!getFreeGame(gameInfo)) {
				return;
			}

		}

		
		
		gameTagLinkInfo.setGameSortNo(gameCount);
		gameTagLinkInfo.setgId(gId);
		gameTagLinkInfo.setTagId(tagId);
		gameTagLinkInfo.setEnable(1);
		dao.setGameTagLinkDao(gameTagLinkInfo);
	}

	public boolean getFreeGame(GameInfo gameInfo) {
		NetworkType type = gameInfo.getNetworkType();
		if(type==null){
			return false;
		}
		boolean flag = true;
		if (gameInfo.getIsInpackage() == 1) {
			flag = false;
		} else if (type.value() == 11
				&& (gameInfo.getPayCode() == 12 || gameInfo.getPayCode() == 9
						|| gameInfo.getPayCode() == 1 || gameInfo.getPayCode() == 2)) {
			flag = false;
		} else if (type.value() == 11 && gameInfo.getPayCode() == 0) {
			flag = true;
		} else if (type.value() == 11 && gameInfo.getPayCode() > 0) {
			flag = false;
		} else if (type.value() == 12 && gameInfo.getPayCode() == 5) {
			flag = true;
		} else if (type.value() == 12
				&& (gameInfo.getPayCode() == 13 || gameInfo.getPayCode() == 10
						|| gameInfo.getPayCode() == 3 || gameInfo.getPayCode() == 4)) {
			flag = false;
		} else if (type.value() == 12) {
			flag = false;
		} else if (type.value() == 20
				&& (gameInfo.getPayCode() == 7 || gameInfo.getPayCode() == 6
						|| gameInfo.getPayCode() == 14 || gameInfo.getPayCode() == 8)) {
			flag = false;
		} else if (type.value() == 20 && gameInfo.getPayCode() == 11) {
			flag = true;
		} else if (type.value() == 20) {
			flag = false;
		} else {
			flag = true;
		}
		return flag;
	}
	
	public static void main(String[] args) throws RemoteException {
		GameInfo gameInfo = EGameClientV2.getInstance().getGameInfoById(0, 0,
				238708);
		System.out.println(gameInfo);
	}
}
